package controllers

import (
	"lsis/models"
	"strings"

	"github.com/astaxie/beego"
)

type OrderController struct {
	BaseController
}

func (c *OrderController) URLMapping() {
	c.Mapping("query", c.Query)
	c.Mapping("create", c.Create)
	c.Mapping("cancel", c.Cancel)
	c.Mapping("pay", c.Pay)
	c.Mapping("startServe", c.StartService)
	c.Mapping("callService", c.CallService)
	c.Mapping("finishService", c.FinishService)
	c.Mapping("queryToServe", c.QueryToServe)

}

// @Title 查询订单
// @Description 查询自己的所有订单
// @Param   location   formData   string  true       "用户当前坐标,如:22.676114,113.9048777"
// @Param   session    formData   string  true       "session"
// @Success 200 {object}  models.OrderJson true "用户信息数组 数组"
// @Failure 400
// @Failure 404
// @router /query [post]
func (o *OrderController) Query() {

	_, res, err := models.QueryOrdersByUserId(o.user.Id)
	if err != nil {
		beego.Error("get order failed:", err)
		o.returnResult(errCodeGetUser, err.Error(), "")
	} else {
		o.returnResult(0, "success", res)
	}
}

// @Title 查询订单
// @Description 查询店铺或活动的排队订单，用于叫号
// @Param   location   formData   string  true       "用户当前坐标,如:22.676114,113.9048777"
// @Param   session    formData   string  true       "session"
// @Param   orderType  formData   string  true       "订单类型:active/store"
// @Param   id         formData   int     true       "id号，查询活动id/店铺Id"
// @Success 200 {object}  models.OrderJson true "用户信息数组 数组"
// @Failure 400
// @Failure 404
// @router /queryToServe [post]
func (o *OrderController) QueryToServe() {

	id, err := o.GetInt("id")
	if err != nil {
		beego.Error("Id is invalid", err)
		o.returnResult(errCodeOrder, "Id is invalid", 0)
		return
	}

	orderType := o.GetString("orderType")

	if nil == o.user.Statff {
		beego.Error("staff Id is invalid", err)
		o.returnResult(errCodeOrder, "你不是美妆师", 0)
		return
	}

	_, res, err := models.QueryOrdersByType(id, orderType)
	if err != nil {
		beego.Error("get order failed:", err)
		o.returnResult(errCodeGetUser, err.Error(), "")
	} else {
		o.returnResult(0, "success", res)
	}
}

// @Title 预约
// @Description 用户发起预约，即创建一笔订单
// @Param   location   formData   string  true       "用户当前坐标,如:22.676114,113.9048777"
// @Param   session    formData   string  true       "session"
// @Param   storeId    formData   int  true          "店铺Id号"
// @Param   staffId    formData   int  true          "预约的美妆师Id"
// @Param   production    formData   string  true    "选择的消费商品：1:美甲；2:美睫；3:护肤；4:纹眉；5:小姐姐纹身，多个商品，用英文逗号分隔"
// @Success 200 {object} controllers.result
// @Failure 400
// @Failure 404
// @router /create [post]
func (o *OrderController) Create() {

	storeId, err := o.GetInt("storeId")
	if err != nil {
		beego.Error("get storeId failed:", err)
		o.returnResult(errCodeOrder, err.Error(), "")
		return
	}

	staffId, err := o.GetInt("staffId")
	if err != nil {
		beego.Error("get staffId failed:", err)
		o.returnResult(errCodeOrder, err.Error(), "")
		return
	}

	production := o.GetString("production")
	_, err = models.AddNewOrder(o.user.Id, storeId, staffId, production)
	if err != nil {
		beego.Error("create order failed:", err)
		o.returnResult(errCodeOrder, err.Error(), "")
	} else {
		o.returnResult(success, "success", "")
	}

}

// @Title 取消预约
// @Description 用户发起预约，即创建一笔订单
// @Param   location   formData   string  true       "用户当前坐标,如:22.676114,113.9048777"
// @Param   session    formData   string  true       "session"
// @Param   orderId    formData   int  true          "订单号"
// @Success 200 {object} controllers.result
// @Failure 400
// @Failure 404
// @router /cancel [post]
func (o *OrderController) Cancel() {
	orderId, err := o.GetInt("orderId")
	if err != nil {
		beego.Error("create order failed:", err)
		o.returnResult(errCodeOrder, err.Error(), "")
	}

	err = models.CancelOrder(orderId, o.user.Id)
	if err != nil {
		beego.Error("cancel order failed:", err)
		o.returnResult(errCodeOrder, err.Error(), "please try again")
	} else {
		o.returnResult(success, "success", "")
	}
}

// @Title 订单支付
// @Description 用户发起预约，即创建一笔订单
// @Param   location   formData   string  true       "用户当前坐标,如:22.676114,113.9048777"
// @Param   session    formData   string  true       "session"
// @Param   orderId    formData   int  true          "订单号"
// @Success 200 {object} controllers.result
// @Failure 400
// @Failure 404
// @router /pay [post]
func (o *OrderController) Pay() {

	ips := strings.Split(o.BaseController.Ctx.Request.RemoteAddr, ":")
	if len(ips) == 0 {
		beego.Error("get client ip failed", o.BaseController.Ctx.Request.RemoteAddr)
		o.returnResult(errCodeOrder, "ip address invalid", 0)
		return
	}

	orderId, err := o.GetInt("orderId")
	if err != nil {
		beego.Error("order Id is invalid", err)
		o.returnResult(errCodeOrder, "order Id is invalid", 0)
		return
	}

	models.PaymentRequestToWepay(o.user.OpenId, ips[0], orderId)
}

// @Title 开始服务
// @Description 美妆师开始服务
// @Param   location   formData   string  false       "用户当前坐标,如:22.676114,113.9048777"
// @Param   session    formData   string  true       "session"
// @Param   orderId    formData   int  true          "订单号"
// @Success 200 {object} controllers.result
// @Failure 400
// @Failure 404
// @router /startService [post]
func (o *OrderController) StartService() {

	orderId, err := o.GetInt("orderId")
	if err != nil {
		beego.Error("order Id is invalid", err)
		o.returnResult(errCodeOrder, "order Id is invalid", 0)
		return
	}

	if nil == o.user.Statff {
		beego.Error("staff Id is invalid", err)
		o.returnResult(errCodeOrder, "你不是美妆师", 0)
		return
	}

	err = models.StartServe(orderId)
	if err != nil {
		beego.Error("staff Id is invalid", err)
		o.returnResult(errCodeOrder, err.Error(), "")
		return
	}
	o.returnResult(0, "success", "")
	return

}

// @Title 叫号
// @Description 美妆师叫号
// @Param   location   formData   string  false       "用户当前坐标,如:22.676114,113.9048777"
// @Param   session    formData   string  true       "session"
// @Param   orderId    formData   int  true          "订单号"
// @Success 200 {object} controllers.result
// @Failure 400
// @Failure 404
// @router /callService [post]
func (o *OrderController) CallService() {

	orderId, err := o.GetInt("orderId")
	if err != nil {
		beego.Error("order Id is invalid", err)
		o.returnResult(errCodeOrder, "order Id is invalid", 0)
		return
	}

	if nil == o.user.Statff {
		beego.Error("staff Id is invalid", err)
		o.returnResult(errCodeOrder, "你不是美妆师", 0)
		return
	}

	err = models.CallServe(orderId, o.user.Statff)
	if err != nil {
		beego.Error("staff Id is invalid", err)
		o.returnResult(errCodeOrder, err.Error(), "")
		return
	}
	o.returnResult(0, "success", "")
	return

}

// @Title 服务完成
// @Description 美妆师完成服务
// @Param   location   formData   string  false      "用户当前坐标,如:22.676114,113.9048777"
// @Param   session    formData   string  true       "session"
// @Param   orderId    formData   int  true          "订单号"
// @Param   cost       formData   int  true          "消费金额"
// @Success 200 {object} controllers.result
// @Failure 400
// @Failure 404
// @router /finishService [post]
func (o *OrderController) FinishService() {

	orderId, err := o.GetInt("orderId")
	if err != nil {
		beego.Error("order Id is invalid", err)
		o.returnResult(errCodeOrder, "order Id is invalid", 0)
		return
	}

	if nil == o.user.Statff {
		beego.Error("staff Id is invalid", err)
		o.returnResult(errCodeOrder, "你不是美妆师", 0)
		return
	}

	cost, err := o.GetInt("cost")
	if err != nil {
		beego.Error("cost  is invalid", err)
		o.returnResult(errCodeOrder, "请输入正确的金额", 0)
		return
	}

	err = models.ServeDone(orderId, int32(cost))
	if err != nil {
		beego.Error("staff Id is invalid", err)
		o.returnResult(errCodeOrder, err.Error(), "")
		return
	}
	o.returnResult(0, "success", "")
	return

}
